home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example8.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  224 lines

  1. /*
  2. **    example8.c
  3. **
  4. ** This example illustrates how to use remote procedure calls
  5. ** and how to process return parameter values from stored
  6. ** procedures.  
  7. **
  8. ** The example uses the following stored procedure, 
  9. ** named "rpctest", which it assumes is located in the 
  10. ** user's default database.  Before running this example, 
  11. ** you must create "rpctest" in your default database.
  12. **
  13. **     create procedure rpctest
  14. **         (@param1 int out,
  15. **          @param2 int out,
  16. **          @param3 int out,
  17. **          @param4 int)
  18. **     as
  19. **     begin
  20. **         select "rpctest is running."
  21. **         select @param1 = 11
  22. **         select @param2 = 22
  23. **         select @param3 = 33
  24. **         select @param1
  25. **
  26. **         return 123
  27. **     end
  28. **
  29. */
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <sybfront.h>
  34. #include <sybdb.h>
  35.  
  36. #define FMTSTR    "%-8.8s %-8.8s %-8.8s %-8.8s\n"
  37. #define FMTSTR1    "%-8.8s %-8.8s %8.8ld %8.8ld\n"
  38.  
  39. /* forward declarations of the error handler and message handler routines. */
  40. int           err_handler( );
  41. int           msg_handler( );
  42.  
  43. main()
  44. {
  45.     LOGINREC         *login;
  46.     DBPROCESS        *dbproc;
  47.  
  48.     int              i;
  49.     int              numrets;
  50.     DBINT            param1 = 1;
  51.     DBINT            param2 = 2;
  52.     DBINT            param3 = 3;
  53.     DBINT            param4 = 4;
  54.     RETCODE          return_code;
  55.  
  56.     /* Initialize DB-Library. */
  57.     if (dbinit() == FAIL)
  58.         exit(ERREXIT);
  59.  
  60.     /* Install the user-supplied error-handling and message-handling
  61.      * routines. They are defined at the bottom of this source file.
  62.      */
  63.     dberrhandle(err_handler);
  64.     dbmsghandle(msg_handler);
  65.     
  66.     /* Allocate and initialize the LOGINREC structure to be used
  67.      * to open a connection to SQL Server.
  68.      */
  69.  
  70.     login = dblogin( );
  71.     DBSETLPWD(login, "server_password");
  72.     DBSETLAPP(login, "rpcexample");
  73.     
  74.     dbproc = dbopen(login, (char *)NULL);
  75.  
  76.     /* Make the rpc. */
  77.     if (dbrpcinit(dbproc, "rpctest", (DBSMALLINT)0) == FAIL)
  78.     {
  79.         printf("dbrpcinit failed.\n");
  80.         dbexit();
  81.         exit(ERREXIT);
  82.     }
  83.  
  84.     if (dbrpcparam
  85.            (dbproc, "@param1", (BYTE)DBRPCRETURN, SYBINT4, -1, -1, ¶m1)
  86.        == FAIL)
  87.     {
  88.         printf("dbrpcparam failed.\n");
  89.         dbexit();
  90.         exit(ERREXIT);
  91.     }
  92.  
  93.     if (dbrpcparam(dbproc, "@param2", (BYTE)0, SYBINT4, -1, -1, ¶m2)
  94.        == FAIL)
  95.     {
  96.         printf("dbrpcparam failed.\n");
  97.         dbexit();
  98.         exit(ERREXIT);
  99.     }
  100.  
  101.     if (dbrpcparam
  102.            (dbproc, "@param3", (BYTE)DBRPCRETURN, SYBINT4, -1, -1, ¶m3)
  103.        == FAIL)
  104.     {
  105.         printf("dbrpcparam failed.\n");
  106.         dbexit();
  107.         exit(ERREXIT);
  108.     }
  109.  
  110.     if (dbrpcparam(dbproc, "@param4", (BYTE)0, SYBINT4, -1, -1, ¶m4)
  111.        == FAIL)
  112.     {
  113.         printf("dbrpcparam failed.\n");
  114.         dbexit();
  115.         exit(ERREXIT);
  116.     }
  117.  
  118.     if (dbrpcsend(dbproc) == FAIL)
  119.     {
  120.         printf("dbrpcsend failed.\n");
  121.         dbexit();
  122.         exit(ERREXIT);
  123.     }
  124.  
  125.     if (dbsqlok(dbproc) == FAIL)
  126.     {
  127.         printf("dbsqlok failed.\n");
  128.         dbexit();
  129.         exit(ERREXIT);
  130.     }
  131.  
  132.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  133.     {
  134.         if (return_code == FAIL)
  135.         {
  136.             printf("dbresults failed.\n");
  137.             dbexit();
  138.             exit(ERREXIT);
  139.         }
  140.  
  141.         /* Print any rows that may have been returned. */
  142.         dbprrow(dbproc);
  143.  
  144.         /* Examine any return parameters that may have arrived. */
  145.  
  146.         numrets = dbnumrets(dbproc);
  147.         printf("%d return values received.\n\n", numrets);
  148.  
  149.         if (numrets > 0)
  150.         {
  151.             printf
  152.              (FMTSTR, "Name", "Type", "Length", "Value");
  153.             printf
  154.                 (FMTSTR,
  155.                  "------------", "------------",
  156.                  "------------", "------------");
  157.         }
  158.  
  159.         for (i = 1; i <= numrets; i++)
  160.         {
  161.             printf
  162.                 (FMTSTR1, dbretname(dbproc, i),
  163.                  dbprtype(dbrettype(dbproc, i)), dbretlen(dbproc, i),
  164.                  *((DBINT *)(dbretdata(dbproc, i))));
  165.         }
  166.  
  167.         if (dbhasretstat(dbproc))
  168.             printf("Return status = %ld\n", dbretstatus(dbproc));
  169.         else
  170.             printf("No return status for this command.\n");
  171.     }
  172.  
  173.     dbexit();
  174. }
  175.  
  176. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  177. DBPROCESS       *dbproc;
  178. int             severity;
  179. int             dberr;
  180. int             oserr;
  181. char            *dberrstr;
  182. char            *oserrstr;
  183. {
  184.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  185.         return(INT_EXIT);
  186.     else 
  187.     {
  188.         printf("DB-Library error:\n\t%s\n", dberrstr);
  189.  
  190.         if (oserr != DBNOERR)
  191.             printf("Operating-system error:\n\t%s\n", oserrstr);
  192.  
  193.         return(INT_CANCEL);
  194.     }
  195. }
  196.  
  197. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  198.                 srvname, procname, line)
  199.  
  200. DBPROCESS       *dbproc;
  201. DBINT           msgno;
  202. int             msgstate;
  203. int             severity;
  204. char            *msgtext;
  205. char            *srvname;
  206. char            *procname;
  207. DBUSMALLINT     line;
  208.  
  209. {
  210.     printf ("Msg %ld, Level %d, State %d\n", 
  211.             msgno, severity, msgstate);
  212.  
  213.     if (strlen(srvname) > 0)
  214.         printf ("Server '%s', ", srvname);
  215.     if (strlen(procname) > 0)
  216.         printf ("Procedure '%s', ", procname);
  217.     if (line > 0)
  218.         printf ("Line %d", line);
  219.  
  220.     printf("\n\t%s\n", msgtext);
  221.  
  222.     return(0);
  223. }
  224.